
package com.wyzz.hr.action;

import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IEmpSalaryAcctBo;
import com.wyzz.hr.bo.IEmpSalaryAcctversionBo;
import com.wyzz.hr.pojo.Empsalaryacct;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarydatadef;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Interpreter;
import com.wyzz.hr.util.StringUtil;

public class AddEmpsalaryacct extends BaseAction {

	public AddEmpsalaryacct() {
		empsalaryacctversion = new Empsalaryacctversion();
		msgNoName = "\u8BF7\u8BBE\u7F6E\u5E10\u5957\u540D\u79F0\uFF01";
		msgDupliName = "\u5E10\u5957\u540D\u79F0\u91CD\u590D\uFF01";
		msgNotComplete = "\u9879\u76EE\u79CD\u7C7B\u4E0D\u5B8C\u6574\uFF0C\u5E10\u5957\u5FC5\u987B\u5305\u542B\u201C{0}\u201D\uFF01";
		msgIllegalForm = "\u60A8\u8F93\u5165\u7684 A{0}\u516C\u5F0F\u4E0D\u5408\u6CD5\uFF1A{1}";
		msgSucc = "{0}\u5DF2\u521B\u5EFA\u6210\u529F\uFF01";
	}

	public String execute() throws Exception {
		if (empsalaryacct == null)
			return "input";
		if (empsalaryacct.getEsacName() == null) {
			addActionErrorInfo(msgNoName, new Object[0]);
			return "input";
		}
		empsalaryacct.setEsacName(empsalaryacct.getEsacName().trim());
		String info = commonCheck();
		if (!info.equals("SUCC")) {
			addErrorInfo(info);
			return "input";
		} else {
			List newItems = getNewAcctItems();
			empsalaryacctversion.setEsavEsac(empsalaryacct);
			Date today = new Date();
			String curempId = getCurrentEmpNo();
			empsalaryacctversion.setEsavCreateBy(curempId);
			empsalaryacctversion.setEsavCreateTime(today);
			empsalaryacctversion.setEsavLastChangeBy(curempId);
			empsalaryacctversion.setEsavLastChangeTime(today);
			empsalaryacctversion.setEsavValidFrom(DateUtil
					.convDateTimeToDate(today));
			empsalaryacctversion.setEsavValidTo(null);
			IEmpSalaryAcctversionBo esavBo = (IEmpSalaryAcctversionBo) getBean("empsalaryacctversionBo");
			esavBo.addAcctVersion(empsalaryacctversion, newItems);
			addSuccessInfo(msgSucc,
					new Object[] { empsalaryacct.getEsacName() });
			empsalaryacct = null;
			return "success";
		}
	}

	private String commonCheck() {
		IEmpSalaryAcctBo esaBo = (IEmpSalaryAcctBo) getBean("empsalaryacctBo");
		if (esaBo.searchAcctNames(empsalaryacct.getEsacName(), null))
			return StringUtil.message(msgDupliName, new Object[0]);
		int i = dataDefIds.length;
		do {
			if (i <= 0)
				break;
			if (dataDefIds[i - 1] != null && dataDefIds[i - 1].length() != 0) {
				acctitemNos = i;
				break;
			}
			i--;
		} while (true);
		Boolean hasBasicSalary = Boolean.valueOf(false);
		Boolean hasIncomeBfTax = Boolean.valueOf(false);
		Boolean hasTax = Boolean.valueOf(false);
		Boolean hasIncomeAfTax = Boolean.valueOf(false);
		Interpreter interpreter = new Interpreter();
		for (i = 0; i < acctitemNos; i++) {
			if (dataTypes[i].intValue() == 1) {
				hasBasicSalary = Boolean.valueOf(true);
				continue;
			}
			if (dataTypes[i].intValue() == 8) {
				hasIncomeBfTax = Boolean.valueOf(true);
				continue;
			}
			if (dataTypes[i].intValue() == 18) {
				hasTax = Boolean.valueOf(true);
				continue;
			}
			if (dataTypes[i].intValue() == 19)
				hasIncomeAfTax = Boolean.valueOf(true);
		}

		if (!hasBasicSalary.booleanValue())
			return StringUtil.message(msgNotComplete,
					new Object[] { "\u57FA\u672C\u5DE5\u8D44" });
		if (!hasIncomeBfTax.booleanValue())
			return StringUtil.message(msgNotComplete,
					new Object[] { "\u7A0E\u524D\u6536\u5165" });
		if (!hasTax.booleanValue())
			return StringUtil.message(msgNotComplete,
					new Object[] { "\u6240\u5F97\u7A0E" });
		if (!hasIncomeAfTax.booleanValue())
			return StringUtil.message(msgNotComplete,
					new Object[] { "\u7A0E\u540E\u6536\u5165" });
		StringBuffer formularError = new StringBuffer();
		for (i = 0; i < acctitemNos; i++) {
			if (dataIsCalcs[i].intValue() != 2)
				continue;
			String info = interpreter.formulaValidate(dataCalcs[i],
					new Empsalaryconfig());
			if (!info.equals("SUCC"))
				formularError.append(StringUtil.message(msgIllegalForm,
						new Object[] { Integer.valueOf(i + 1), info }));
		}

		if (formularError.toString().length() > 0)
			return formularError.toString();
		else
			return "SUCC";
	}

	private List getNewAcctItems() {
		List returnList = new ArrayList();
		for (int i = 0; i < acctitemNos; i++) {
			if (dataDefIds[i] == null || dataDefIds[i].length() == 0 || i >= 48)
				continue;
			Empsalaryacctitems empsalaryacctitem = new Empsalaryacctitems();
			empsalaryacctitem.setEsaiEsdd(new Empsalarydatadef(dataDefIds[i]));
			empsalaryacctitem.setEsaiDataIsCalc(dataIsCalcs[i]);
			if (dataCalcs[i] != null)
				empsalaryacctitem.setEsaiDataCalc(dataCalcs[i].replaceAll(" ",
						"").toUpperCase());
			empsalaryacctitem.setEsaiDataRounding(dataRoundings[i]);
			empsalaryacctitem.setEsaiDataSeq(Integer.valueOf(i + 1));
			empsalaryacctitem.setEsaiEsav(empsalaryacctversion);
			returnList.add(empsalaryacctitem);
		}

		return returnList;
	}

	public void validate() {
	}

	public Empsalaryacct getEmpsalaryacct() {
		return empsalaryacct;
	}

	public void setEmpsalaryacct(Empsalaryacct empsalaryacct) {
		this.empsalaryacct = empsalaryacct;
	}

	public String[] getDataDefIds() {
		return dataDefIds;
	}

	public void setDataDefIds(String dataDefIds[]) {
		this.dataDefIds = dataDefIds;
	}

	public Integer[] getDataIsCalcs() {
		return dataIsCalcs;
	}

	public void setDataIsCalcs(Integer dataIsCalcs[]) {
		this.dataIsCalcs = dataIsCalcs;
	}

	public String[] getDataCalcs() {
		return dataCalcs;
	}

	public void setDataCalcs(String dataCalcs[]) {
		this.dataCalcs = dataCalcs;
	}

	public Integer[] getDataRoundings() {
		return dataRoundings;
	}

	public void setDataRoundings(Integer dataRoundings[]) {
		this.dataRoundings = dataRoundings;
	}

	public Empsalaryacctversion getEmpsalaryacctversion() {
		return empsalaryacctversion;
	}

	public void setEmpsalaryacctversion(
			Empsalaryacctversion empsalaryacctversion) {
		this.empsalaryacctversion = empsalaryacctversion;
	}

	public Integer[] getDataTypes() {
		return dataTypes;
	}

	public void setDataTypes(Integer dataTypes[]) {
		this.dataTypes = dataTypes;
	}

	private static final long serialVersionUID = 1L;
	private Empsalaryacct empsalaryacct;
	private Empsalaryacctversion empsalaryacctversion;
	private String dataDefIds[];
	private Integer dataTypes[];
	private Integer dataIsCalcs[];
	private String dataCalcs[];
	private Integer dataRoundings[];
	private int acctitemNos;
	private String msgNoName;
	private String msgDupliName;
	private String msgNotComplete;
	private String msgIllegalForm;
	private String msgSucc;
}
